<template>
  <div class="page-user-login">
    <van-nav-bar
      left-arrow
      @click-left="$router.back()"
      title="登录"
    ></van-nav-bar>

    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="mobile"
        label="手机号"
        placeholder="请输入手机号"
        :rules="[
          { required: true, message: '请填写手机号' },
          { pattern, message: '请输入正确内容' },
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="code"
        label="验证码"
        placeholder="请输入验证码"
        :rules="[
          { required: true, message: '请填写密码' },
          { validator, message: '请输入正确内容' },
        ]"
      >
        <template #button>
          <van-button size="small" type="primary" native-type="button"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  name: 'user-chat',
  data () {
    return {
      username: '15632012564',
      password: '246810',
      pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
    }
  },
  methods: {
    async onSubmit (values) {
      console.log('submit', values)
      try {
        await this.$store.dispatch('setToken', values)
        this.$toast.success('登陆成功')
        this.$router.push(this.$route.query.redirectUrl || '/')
      } catch (err) {
        this.$toast.fail(err.response.data.message)
      }
    },
    validator (val) {
      console.log('当前校验表单项的值：', val)
      if (val.length !== 6) return false
      return true
    }
  }
}
</script>

<style scoped lang='less'></style>
